package cn.haohaowo.spring.chapter4;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import junit.framework.Assert;

import org.jboss.vfs.TempFileProvider;
import org.jboss.vfs.VFS;
import org.jboss.vfs.VirtualFile;
import org.jboss.vfs.spi.RealFileSystem;
import org.junit.Test;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.VfsResource;

public class ResourceTest {
	
//	@Test
	public void testByteArrayResource() {
		Resource resource = new ByteArrayResource("Hello world!".getBytes());
		if(resource.exists()) {
			dumpStream(resource);
		}
	}
	
//	@Test
	public void testInputStreamResource() {
		InputStream is = new ByteArrayInputStream("Hello world!".getBytes());
		Resource resource = new InputStreamResource(is);
		if(resource.exists()) {
			dumpStream(resource);
		}
		Assert.assertEquals(true, resource.isOpen());
	}
	
//	@Test
	public void testFileResource() {
		File file = new File("/home/robinson/Downloads/1.txt");
		Resource resource = new FileSystemResource(file);
		if(resource.exists()) {
			dumpStream(resource);
		}
		Assert.assertEquals(false, resource.isOpen());
	}
	
//	@Test
	public void testClasspathResourceByDefaultClassLoader() throws IOException {
		Resource resource = new ClassPathResource("log4j.properties");
		if(resource.exists()) {
			dumpStream(resource);
		}
		System.out.println("path" + resource.getFile().getAbsolutePath());
		Assert.assertEquals(false, resource.isOpen());
	}
	
//	@Test
	public void testClasspathResourceByClassLoader() throws IOException {
		ClassLoader cl = this.getClass().getClassLoader();
		Resource resource = new ClassPathResource("log4j.properties", cl);
		if(resource.exists()) {
			dumpStream(resource);
		}
		System.out.println("path" + resource.getFile().getAbsolutePath());
		Assert.assertEquals(false, resource.isOpen());
	}
	
//	@Test
	public void testClasspathResourceByClass() throws IOException {
		Class clazz = this.getClass();
		Resource resource1 = new ClassPathResource("ResourceTest.java", clazz);
		if(resource1.exists()) {
			dumpStream(resource1);
		}
		System.out.println("path" + resource1.getFile().getAbsolutePath());
		Assert.assertEquals(false, resource1.isOpen());
		
		Resource resource2 = new ClassPathResource("ResourceTest.java", this.getClass());
		if(resource1.exists()) {
			dumpStream(resource2);
		}
		System.out.println("path" + resource2.getFile().getAbsolutePath());
		Assert.assertEquals(false, resource2.isOpen());
	}
	
//	@Test
	public void testClasspathResourceTestFromJar() throws IOException {
		Resource resource = new ClassPathResource("overview.html");
		if(resource.exists()) {
			dumpStream(resource);
		}
		System.out.println("path" + resource.getURL().getPath());
		Assert.assertEquals(false, resource.isOpen());
	}
	
//	@Test
	public void testUrlResource() throws IOException {
		Resource resource = new UrlResource("http://image.baidu.com/i?fr=hotindex&tn=baiduimage&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E4%BF%84%E7%BE%8E%E5%A5%B3%E7%A9%BA%E9%99%8D%E5%85%B5");
		if(resource.exists()) {
			dumpStream(resource);
		}
		System.out.println("path" + resource.getURL().getPath());
		Assert.assertEquals(false, resource.isOpen());
	}
	
//	@Test
	public void testVfsResourceForRealFileSytem() throws IOException {
		VirtualFile home = VFS.getChild("/home");
		VFS.mount(home, new RealFileSystem(new File("/home/robinson/Downloads")));
		VirtualFile testFile = home.getChild("1.txt");
		Resource resource = new VfsResource(testFile);
		if(resource.exists()) {
			dumpStream(resource);
		}
		System.out.println("path:" + resource.getFile().getAbsolutePath());
		Assert.assertEquals(false, resource.isOpen());
	}

//	@Test
	public void testVfsResourceForJar() throws IOException {
		File realFile = new File("spring-beans-3.1.1.RELEASE.jar");
		VirtualFile home = VFS.getChild("/home2");
		VFS.mountZipExpanded(realFile, home, TempFileProvider.create("temp", 
				Executors.newScheduledThreadPool(1)));
		VirtualFile testFile = home.getChild("META-INF/spring.handlers");
		Resource resource = new VfsResource(testFile);
		if(resource.exists()) {
			dumpStream(resource);
		}
		System.out.println("path:" + resource.getFile().getAbsolutePath());
		Assert.assertEquals(false, resource.isOpen());
	}
	
	@Test
	public void testResourceLoad() {
		ResourceLoader loader = new DefaultResourceLoader();
		Resource resource = loader.getResource("classpath:log4j.properties");
		Assert.assertEquals(ClassPathResource.class, resource.getClass());
		
		Resource resource2 = loader.getResource("file:log4j.properties");
		Assert.assertEquals(UrlResource.class, resource2.getClass());
		
		Resource resource3 = loader.getResource("log4j.properties");
		Assert.assertTrue(resource3 instanceof ClassPathResource);
	}
	
	private void dumpStream(Resource resource) {
		InputStream is = null;
		try {
			is = resource.getInputStream();
			byte[] descBytes = new byte[is.available()];
			is.read(descBytes);
			System.out.println(new String(descBytes));
		} catch(IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch(IOException e) {
				
			}
		}
	} 
}
